package cn.hzw.supermarket.activity;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.databinding.ActivityMainBinding;
import cn.hzw.supermarket.fragment.HomeFragment;
import cn.hzw.supermarket.fragment.MeFragment;
import cn.hzw.supermarket.fragment.ShopCartFragment;

public class MainActivity extends FragmentActivity {

    private FragmentManager fManager;
    //主页
    private HomeFragment homeFragment;
    //购物清单
    private ShopCartFragment shopCartFragment;
    //个人中心
    private MeFragment meFragment;
    private ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏，隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initViews();
        clickMenu(binding.llMenuHome);
    }

    /**
     * 初始化控件
     */
    private void initViews() {
        fManager = getSupportFragmentManager();
    }

    public void clickMenu(View view) {
        FragmentTransaction transaction = fManager.beginTransaction();
        int vID = view.getId();
        setMenuStyle(vID);
        hideFragment(transaction);

        setFragment(vID,transaction);
        transaction.commit();
    }

    //隐藏其他fragment布局
    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment!=null){
            transaction.hide(homeFragment);
        }
        if (shopCartFragment!=null){
            transaction.hide(shopCartFragment);
        }
        if (meFragment!=null){
            transaction.hide(meFragment);
        }
    }

    //设置导航栏点击布局
    private void setMenuStyle(int vID) {
        if (vID==R.id.ll_menu_home){
            binding.ivMenuHome.setImageDrawable(getResources().getDrawable(R.mipmap.menu_home_click));
            binding.tvMenuHome.setTextColor(getResources().getColor(R.color.colorPrimary));
        }else {
            binding.ivMenuHome.setImageDrawable(getResources().getDrawable(R.mipmap.menu_home));
            binding.tvMenuHome.setTextColor(getResources().getColor(R.color.menu_nomarl));
        }
        if (vID==R.id.ll_menu_talk){
            binding.ivMenuTalk.setImageDrawable(getResources().getDrawable(R.mipmap.menu_talk_click));
            binding.tvMenuTalk.setTextColor(getResources().getColor(R.color.colorPrimary));
        }else {
            binding.ivMenuTalk.setImageDrawable(getResources().getDrawable(R.mipmap.menu_talk));
            binding.tvMenuTalk.setTextColor(getResources().getColor(R.color.menu_nomarl));
        }
        if (vID==R.id.ll_menu_me){
            binding.ivMenuMe.setImageDrawable(getResources().getDrawable(R.mipmap.menu_me_click));
            binding.tvMenuMe.setTextColor(getResources().getColor(R.color.colorPrimary));
        }else {
            binding.ivMenuMe.setImageDrawable(getResources().getDrawable(R.mipmap.menu_me));
            binding.tvMenuMe.setTextColor(getResources().getColor(R.color.menu_nomarl));
        }
    }
    private Long exitTime = 0L;//记录第一次点击时的时间
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if ((System.currentTimeMillis()-exitTime)>2000){
                Toast.makeText(this, "再按一次退出校易购", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }else {
                Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                homeIntent.addCategory(Intent.CATEGORY_HOME);
                homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(homeIntent);
            }
            return true;
        }else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private void setFragment(int vID, FragmentTransaction transaction) {
        switch (vID){
            case R.id.ll_menu_home:
                if (homeFragment == null){
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.content,homeFragment);
                }else {
                    transaction.show(homeFragment);
                }
                break;
            case R.id.ll_menu_talk:
                shopCartFragment = new ShopCartFragment();
                Bundle bundle = new Bundle();
                bundle.putString("key","红心柚子");
                shopCartFragment.setArguments(bundle);
                break;
            case R.id.ll_menu_me:
                if (meFragment==null) {
                    meFragment = new MeFragment();
                    transaction.add(R.id.content, meFragment);
                }else {
                    transaction.show(meFragment);
                }
                break;
            default:
                break;
        }
    }

}